package com.tjian.fafa.user.base.mainBody;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.base.ConnectedApp;
import com.tjian.fafa.user.base.bean.UserInfo;
import com.tjian.fafa.user.base.chatServices.ChatService;
import com.tjian.fafa.user.base.chatServices.ChatServiceData;
import com.tjian.fafa.user.base.chatServices.FriendListInfo;

import java.util.ArrayList;
import java.util.List;

public class FriendListPage implements OnChildClickListener
{

    private static FriendListPage mInstance;

    private View mViewOfPage;
    private Context mContext0;

    private Button mBtnBack;
    private PatchedExpandableListView mListView = null;
    private FriendListAdapter mAdapter = null;

    private List<FriendListGroupItem> mGrpInfo = null;
    private List<List<UserInfo>> mUsrInfo = null;

    public static FriendListPage getInstance()
    {
        if (mInstance == null)
        {
            mInstance = new FriendListPage();
        }
        return mInstance;
    }

    public void onInit(View view, Context cont)
    {
        mViewOfPage = view;
        mContext0 = cont;
    }

    public void onCreate()
    {
        mInstance = this;
        mBtnBack = (Button) mViewOfPage.findViewById(R.id.cc0_friend_list_btn_back);
        mListView = (PatchedExpandableListView) mViewOfPage.findViewById(R.id.cc0_friend_list_listview);
        //mListView.setGroupIndicator(mContext0.getResources().getDrawable(R.drawable.expander_floder));
        mListView.setGroupIndicator(null);
        mBtnBack.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

            }
        });
        UserInfo userInfo = ConnectedApp.getInstance().getUserInfo();
        if (userInfo != null)
        {
            onFriendListUpdate();
            mListView.expandGroup(0);
        }
    }

    /*
     * ChildView 设置 布局很可能onChildClick进不来，要在 ChildView layout 里加上
     * android:descendantFocusability="blocksDescendants",
     * 还有isChildSelectable里返回true
     */
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
                                int groupPosition, int childPosition, long id)
    {
        int pos = childPosition;
        int id_x = ChatServiceData.getInstance().getIdForPos(pos);
        ChatService.getInstance().setType(2);
        ChatService.getInstance().setId(id_x);
        ChatService.getInstance().setEnterFromNotification(false);

        String frilist = mUsrInfo.toString();
        frilist = frilist.substring(2);
        frilist = frilist.substring(0, frilist.length() - 2);
        String[] sbArr0 = frilist.split(",");
        UserInfo firinfo = new UserInfo(sbArr0[childPosition]);
        ChatService.getInstance().setmImgpath(firinfo.getmAvatarImagePath());
        ChatServiceData.getInstance().clearUnreadMsgs(id_x);
        MainBodyActivity.getInstance().gotoChatActivity();
        return true;
    }

    private void initData()
    {
        mGrpInfo = new ArrayList<FriendListGroupItem>();
        mUsrInfo = new ArrayList<List<UserInfo>>();

        List<UserInfo> curListFriends = FriendListInfo.getFriendListInfo().getFriendList();
        mGrpInfo.add(new FriendListGroupItem("我的好友", curListFriends.size()));

        mUsrInfo.add(curListFriends);
    }

    public void onFriendListUpdate()
    {
        initData();
        mAdapter = new FriendListAdapter(mContext0, mGrpInfo, mUsrInfo);
        mListView.setAdapter(mAdapter);
        mListView.setDescendantFocusability(ExpandableListView.FOCUS_AFTER_DESCENDANTS);
        mListView.setOnChildClickListener(this);
    }

}
